package io.jee.runshan.controller.admin;

import io.jee.medusa.orm.condition.Condition;
import io.jee.medusa.orm.condition.ConditionGroup;
import io.jee.medusa.orm.pager.Pager;
import io.jee.medusa.vo.ResultBean;
import io.jee.runshan.domain.QiyeMessageHistory;
import io.jee.runshan.domain.remote.qiye.base.Response;
import io.jee.runshan.domain.remote.qiye.request.NewsMessageArticleData;
import io.jee.runshan.domain.remote.qiye.request.NewsMessageData;
import io.jee.runshan.domain.remote.qiye.request.NewsMessageSendData;
import io.jee.runshan.domain.remote.qiye.request.TextMessageData;
import io.jee.runshan.domain.remote.qiye.request.TextMessageSendData;
import io.jee.runshan.service.IQiyeMessageHistoryService;
import io.jee.runshan.service.remote.qiye.IMessageService;
import io.jee.runshan.service.remote.qiye.impl.MessageServiceImpl;
import io.jee.runshan.utils.WeixinConstant;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.beans.BeansException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/admin/qiye/message/")
public class QiyeMesssageController {
	@Resource
	private IQiyeMessageHistoryService messageHistoryService;
	@Resource
	private IMessageService messageService;
	

	@RequestMapping("/list")
	public ModelAndView list(Pager<QiyeMessageHistory> pager, 
			@RequestParam (defaultValue = "-1", required = true) Integer type, 
			@RequestParam (defaultValue = "1970-01-01", required = true) String stime, 
			@RequestParam (defaultValue = "7015-12-30", required = true) String etime) {
		ConditionGroup group = new ConditionGroup();
		if (type > -1) {
			group.and(Condition.onNumber("type").eq(type));
		}
		group.and(Condition.onNumber("root").eq(true));
		// group.and(Condition.onDate("publishTime").get(DateUtils.parseDate(stime + " 00:00:00", "yyyy-MM-dd HH:mm:ss")));
		// group.and(Condition.onDate("publishTime").let(DateUtils.parseDate(etime + " 23:59:59", "yyyy-MM-dd HH:mm:ss")));
		group.and(Condition.onBool("root").eq(true));
		pager = messageHistoryService.list(pager, group, "desc", "publishTime");
		return new ModelAndView("/admin/qiye/message/list").addObject("type", type)
				.addObject("stime", stime)
				.addObject("etime", etime)
				.addObject("pager", pager);
	}
	
	@RequestMapping(value = "/ajaxMessageById", produces = { "application/json" })
	@ResponseBody
	public List<QiyeMessageHistory> ajaxMessageById(Integer id) {
		List<QiyeMessageHistory> list = null;
		try {
			QiyeMessageHistory history = messageHistoryService.get(id);
			if(history.getType() == 1){
				String publishKey = history.getPublishKey();
				list = messageHistoryService.list("publishKey", publishKey);
			}else{
				list = new ArrayList<QiyeMessageHistory>();
				list.add(history);
			}
		} catch (BeansException e) {
			e.printStackTrace();
		}
		return list;
	}
	
	@RequestMapping("/send")
	public ModelAndView send() {
		return new ModelAndView("/admin/qiye/message/send");
	}
	
	@RequestMapping(value = "/ajaxSend", method = RequestMethod.POST)
	@ResponseBody
	public ResultBean<Response> ajaxSend(QiyeMessageHistory message, String userIds) {
		Response response = null;
		if (message.getType() == 0) { // 文本消息
			IMessageService service = new MessageServiceImpl();
			TextMessageSendData data = new TextMessageSendData();
			data.setMsgtype(TextMessageSendData.TEXT_TYPE);
//			data.setToparty(depts);
			data.setTouser(userIds);
			data.setText(new TextMessageData(message.getContent()));

			response = service.send(data);
			
			QiyeMessageHistory history = new QiyeMessageHistory();
			history.setPublishTime(new Date());
			history.setTarget(message.getTarget());
			history.setContent(message.getContent());
			history.setPublishKey(UUID.randomUUID().toString());
			history.setOrderIndex(1);
			history.setType(message.getType());
			messageHistoryService.save(history);
		} else if (message.getType() == 1) { // 单图文消息
			String uuid = UUID.randomUUID().toString();
			QiyeMessageHistory history = new QiyeMessageHistory();
			history.setPublishTime(new Date());
			history.setTarget(message.getTarget());
			history.setContent(message.getContent());
			history.setPublishKey(uuid);
			history.setOrderIndex(1);
			history.setType(message.getType());
			history.setPicurl(message.getPicurl());
			history.setDescription(message.getDescription());
			history.setTitle(message.getTitle());
			messageHistoryService.save(history);
			
			
			NewsMessageSendData data = new NewsMessageSendData();
			data.setMsgtype(TextMessageSendData.NEWS_TYPE);
//			data.setToparty(depts);
			data.setTouser(userIds);
			
			NewsMessageArticleData article = new NewsMessageArticleData();
			article.setDescription(message.getDescription());
			article.setTitle(message.getTitle());
			article.setPicurl(WeixinConstant.getRootUrl() + message.getPicurl());
			article.setUrl(WeixinConstant.getRootUrl() + "/web/qiye/message/detail?id=" + history.getId());
			
			NewsMessageData articleData = new NewsMessageData();
			articleData.addArticle(article);
			
			data.setNews(articleData);
			
			response = messageService.send(data);
		}
		return ResultBean.result(response.success(), response.getErrmsg());
	}
	
}
